package org.com.ashen.service.service.sysusersmanage;

import java.util.UUID;

import javax.annotation.Resource;

import org.com.ashen.client.service.ISysUsersService;
import org.com.ashen.common.common.HttpResult;
import org.com.ashen.common.common.sysusers.SelectUserReqDTO;
import org.com.ashen.common.common.sysusers.SysUsersDTO;
import org.com.ashen.common.exception.BusinessException;
import org.com.ashen.common.exception.ErrorEnum;
import org.com.ashen.dal.dataobject.SaasAppSession;
import org.com.ashen.dal.dataobject.SysUsers;
import org.com.ashen.service.config.JdbcConfig;
import org.com.ashen.service.dao.SaasAppSessionDao;
import org.com.ashen.service.dao.SysUsersDao;
import org.com.ashen.service.datasourceswitch.DataSourceBeanBuilder;
import org.com.ashen.service.datasourceswitch.DataSourceContext;
import org.springframework.stereotype.Service;


@Service("sysUsersService")
public class SysUsersServiceImpl implements ISysUsersService {
	 @Resource(name = "sysUsersDao")
	 private SysUsersDao sysUsersDao;
	 
	 @Resource(name = "jdbcConfig")
	 private JdbcConfig jdbcConfig;
	 
	 
	 @Resource(name = "saasAppSessionDao")
	 private SaasAppSessionDao saasAppSessionDao;
	 
	
	@Override
	public HttpResult<SysUsersDTO> selectUserByPrimaryKey(SelectUserReqDTO selectUserReqDTO) {
		if (selectUserReqDTO == null) {
			throw new BusinessException(ErrorEnum.PARAM_NULL); 
		}
		String dbname = selectUserReqDTO.getDbname();
		String loginid = selectUserReqDTO.getLoginid();
		String password = selectUserReqDTO.getPassword();

		DataSourceBeanBuilder builder = new DataSourceBeanBuilder(
                 "RTSaas"+ dbname,
                 jdbcConfig.dbUrl,
                 jdbcConfig.dbPort,
                 "RTSaas"+  dbname,
                 jdbcConfig.dbUser,
                 jdbcConfig.dbPass);
         DataSourceContext.setDataSource(builder);
         SysUsers sysUsers = null;
         if (sysUsersDao.getUserByLoginId(loginid) == null) {
        	 throw new BusinessException(ErrorEnum.PASSWORD_NOT_RIGHT); 
		}else {
			sysUsers = sysUsersDao.getUserByLoginId(loginid);
		}
         if (!password.equals(sysUsers.getPassword())) {
        	 throw new BusinessException(ErrorEnum.PASSWORD_NOT_RIGHT); 
		}
         String sessionId = UUID.randomUUID().toString().replaceAll("-", "");
         SysUsersDTO sysUsersDTO = new SysUsersDTO();
         sysUsersDTO.setUserid(sysUsers.getUserid());
         sysUsersDTO.setSessionid(sessionId);
         sysUsersDTO.setUsername(sysUsers.getUsername());
         sysUsersDTO.setViewname("main1");
         sysUsersDTO.setGps("0");
         sysUsersDTO.setGpstime("15");
         
         SaasAppSession saasAppSession = SaasAppSessionConvert.convertToSaasSession(sessionId, selectUserReqDTO, dbname);
        
         DataSourceContext.clearDataSource();
         
         
         
         DataSourceBeanBuilder builder2 = new DataSourceBeanBuilder(
                 jdbcConfig.dbName,
                 jdbcConfig.dbUrl,
                 jdbcConfig.dbPort,
                 jdbcConfig.dbName,
                 jdbcConfig.dbUser,
                 jdbcConfig.dbPass);
         DataSourceContext.setDataSource(builder2);
         saasAppSessionDao.createSaasAppSession(saasAppSession);
         
         DataSourceContext.clearDataSource();
		return HttpResult.successResult(sysUsersDTO);
	}

}
